package Code.Day2_20240919.com.dx.day02;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;

/**
 * @ClassName LambdaPerson
 * @Description Lambda表达式
 * @Author 12994
 * @Date 2024/9/19 18:00
 * @Version 1.0
 **/
@Getter
@AllArgsConstructor
public class LambdaPersonNew {
    private String name;
    private int age;

    @FunctionalInterface
    interface Predicate<T> {
        boolean test(T t);
    }

    @FunctionalInterface
    interface Consumer<T> {
        void accept(T t);
    }

    @Override
    public String toString() {
        return "LambdaPersonNew{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    private static void checkAndPrinter(List<LambdaPersonNew> people,
                                        Predicate<LambdaPersonNew> predicate,
                                        Consumer<LambdaPersonNew> consumer) {
//        for (LambdaPerson p : people) {
//            if (firstNameChecker.makeCheck(p)) {
//                agePrinter.makePrint(p);
//            }
//        }
        people.forEach(p -> {
            if (predicate.test(p)) {
                consumer.accept(p);
            }
        });
    }

    public static void main(String[] args) {
        List<LambdaPersonNew> people = Arrays.asList(
                new LambdaPersonNew("张三", 25),
                new LambdaPersonNew("李四", 40),
                new LambdaPersonNew("张五", 40)
        );


        checkAndPrinter(people, p -> p.name.startsWith("张"),
                p -> System.out.println(p.age));


        people.stream().filter(p -> p.name.startsWith("张")).forEach(System.out::println);
        people.stream().filter(p -> p.name.startsWith("张")).forEach((p) -> {
            System.out.println(p.age);
        });
    }


}
